<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>local manual page - </TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">[incr Tcl] Package Commands, version 4.2.1</a> <small>&gt;</small> local</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
itcl::local &mdash; create an object local to a procedure
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>itcl::local </B><I>className objName</I> ?<I>arg arg ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>local</B> command creates an <B>[incr&nbsp;Tcl]</B> object that
is local to the current call frame.  When the call frame goes away,
the object is automatically deleted.  This command is useful for
creating objects that are local to a procedure.
<P>
As a side effect, this command creates a variable named
&quot;<B>itcl-local-</B><I>xxx</I>&quot;, where <I>xxx</I> is the name of
the object that is created.  This variable detects when the
call frame is destroyed and automatically deletes the
associated object.

<H3><A NAME="M5">EXAMPLE</A></H3>
In the following example, a simple &quot;counter&quot; object is used
within the procedure &quot;test&quot;.  The counter is created as a
local object, so it is automatically deleted each time the
procedure exits.  The <B><A HREF="../TclCmd/puts.htm">puts</A></B> statements included in the
constructor/destructor show the object coming and going
as the procedure is called.
<P>
<PRE>itcl::class counter {
    private variable count 0
    constructor {} {
        puts &quot;created: $this&quot;
    }
    destructor {
        puts &quot;deleted: $this&quot;
    }

    method bump {{by 1}} {
        incr count $by
    }
    method get {} {
        return $count
    }
}

proc test {val} {
    local counter x
    for {set i 0} {$i &lt; $val} {incr i} {
        x bump
    }
    return [x get]
}

set result [test 5]
puts &quot;test: $result&quot;

set result [test 10]
puts &quot;test: $result&quot;

puts &quot;objects: [itcl::find objects *]&quot;</PRE>
<H3><A NAME="M6">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#class">class</A>, <A href="../Keywords/O.htm#object">object</A>, <A href="../Keywords/P.htm#procedure">procedure</A>
<div class="copy">Copyright &copy; 1993-1998 Lucent Technologies, Inc.
</div>
</BODY></HTML>
